package com.zwps.biz.controller.http;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.zwps.biz.api.model.dto.auth.PasswordLoginAuthDTO;
import com.zwps.biz.api.model.dto.user.AddUserDTO;
import com.zwps.biz.api.model.dto.user.SelectUserDTO;
import com.zwps.biz.api.model.dto.user.UpdateUserDTO;
import com.zwps.biz.api.model.vo.auth.AuthInfoVO;
import com.zwps.biz.api.model.vo.permission.page.PagePermissionTreeVO;
import com.zwps.biz.api.model.vo.role.RoleVO;
import com.zwps.biz.api.model.vo.user.UserVO;
import com.zwps.biz.dal.db.dao.UserRoleDAO;
import com.zwps.biz.domain.service.UserService;
import com.zwps.common.api.model.dto.PageInfoDTO;
import com.zwps.common.controller.response.message.ResponseDataMessage;
import com.zwps.common.controller.response.message.ResponseDataPageMessage;
import com.zwps.common.controller.response.message.ResponseMessage;
import com.zwps.common.core.exception.UserActionException;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 用户管理API
 * 
 * @ClassName: UserController
 * @Description:
 * @author lyh
 * @date 2023年5月23日 上午10:52:15
 *
 */
@Tag(name = "用户管理")
@RestController
@Validated
@RequestMapping("system/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleDAO userRoleDAO;

    @Operation(summary = "新增用户")
    @PostMapping
    public void addUser(@Valid @NotNull @RequestBody AddUserDTO dto) {
        userService.addUser(dto);
    }

    @Operation(summary = "分页查询用户")
    @GetMapping("page")
    public ResponseDataPageMessage<UserVO> selectUserPage(SelectUserDTO dto) {
        return ResponseDataPageMessage.data(userService.selectUserPage(dto));
    }

    @Operation(summary = "分页查询角色列表")
    @GetMapping("{userId}/role/page")
    public List<RoleVO> selectRolePage(@Valid @NotBlank @PathVariable String userId, PageInfoDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return userRoleDAO.selectAllRoleByUserId(userId);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("{ids}")
    public void deleteUsers(@Valid @NotBlank @PathVariable String ids) {
        userService.deleteUsers(ids);
    }

    @Parameter(name = "id", description = "用户ID", in = ParameterIn.PATH)
    @Operation(summary = "修改用户信息")
    @PutMapping("{id}")
    public void updateUser(@Valid @NotBlank @PathVariable String id, @Valid @NotNull @RequestBody UpdateUserDTO dto) {
        userService.updateUser(id, dto);
    }

    @GetMapping("{id}/exist")
    public boolean exist(@Valid @NotBlank @PathVariable("id") String id) {
        return userService.exist(id);
    }

    @Operation(summary = "密码重置")
    @GetMapping("{id}/reset")
    public void resetPassWord(@Valid @NotBlank @PathVariable("id") String id) {
         userService.resetPassWord(id);
    }


    @Operation(summary = "账号启用禁用")
    @GetMapping("{id}/status")
    public void setUpStatus(@Valid @NotBlank @PathVariable("id") String id) {
        userService.setUpStatus(id);
    }

    @Operation(summary = "获取下拉所有用户信息")
    @Parameters({
            @Parameter(name = "filtration", description = "是否过滤（默认：不过滤，当为1过滤，只查询所在部门）"),
            @Parameter(name = "projectId", description = "根据项目id过滤")
    })
    @GetMapping("option")
    public List<UserVO> selectAllUser(@RequestParam(required = false) String filtration) {
        return userService.selectAllUser(filtration);
    }
    
    @Operation(summary = "查询当前登录用户的页面权限树")
    @GetMapping("logined/page_permission_tree")
    public List<PagePermissionTreeVO> findLoginedUserPagePermissionTree() {
        return userService.byLoginedUserFindPagePermissionTree();
    }

    @Operation(summary = "根据id获取用户详情")
    @GetMapping("getUserInfoById/{id}")
    public UserVO getUserInfoById(@PathVariable("id") String id) {
        return userService.getUserInfoById(id);
    }

    @Operation(summary = "根据姓名查询所属用户id")
    @GetMapping("getUserInfoByLikeUserName")
    public List<String> getUserInfoByLikeUserName(@RequestParam("userName") String userName) {
        if (StrUtil.isEmpty(userName)) {
            throw new UserActionException("姓名不能为空");
        }
        return userService.getUserInfoByLikeUserName(userName);
    }

    @Operation(summary = "获取当前用户信息")
    @GetMapping("info")
    public AuthInfoVO getCurrentUserInfor(){
        return userService.getCurrentUserInfor();
    }

    /**
     * 返回第三方登录用户密钥
     */
    @Operation(summary = "提供第三方登录密钥")
    @PostMapping("loginSecretKey")
    public ResponseDataMessage<String> loginSecretKey(@Valid @NotNull @RequestBody PasswordLoginAuthDTO loginDTO){
        return ResponseDataMessage.data(userService.loginSecretKey(loginDTO));
    }



}
